package edu.dali.enterprise_test.controller.auth;

import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.model.vo.SysPermission;
import edu.dali.enterprise_test.service.auth.PermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 权限管理
 */
@Controller
@RequestMapping("/permission")
public class PermMgrController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private PermissionService permissionService;


    /**
     * 主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "permission/index";
    }
    @RequestMapping("getPerms")
    @ResponseBody
    public Pager<SysPermission> getPerms(int page , int limit) {
        PageHelper.startPage(page , limit);
        String name = request.getParameter("name");
        String url = request.getParameter("url");

        Dto dto = new Dto();

        if (!StringUtils.isEmpty(name))
            dto.put("name" , name);
        if (!StringUtils.isEmpty(url))
            dto.put("url" , url);
        Pager<SysPermission> p = permissionService.getPermissions(dto);
        return p;
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("addPermission")
    public String addPermission() {
        return "permission/add";
    }

    /**
     * 处理添加权限
     * @param name
     * @param permission
     * @param available
     * @param url
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Map<String , Object> add(String name , String permission ,String available , String url) {
        Map<String , Object> map = new HashMap<>();
        SysPermission perm = new SysPermission();
        perm.setName(name);
        perm.setPermission(permission);
        perm.setUrl(url);
        perm.setResourceType("menu");
        perm.setAvailable(Boolean.valueOf(available));

        permissionService.insertPermission(perm);

        map.put("code" , 0);
        map.put("msg" , "添加成功");
        return map;
    }

    /**
     * 查看权限详情
     * @param permissionId
     * @param model
     * @return
     */
    @RequestMapping("detail/{permissionId}")
    public String detail(@PathVariable Long permissionId , Model model) {
        Dto dto = new Dto();
        dto.put("id" , permissionId);
        SysPermission permission = permissionService.selectPerm(dto);

        model.addAttribute("permission" , permission);
        return "permission/detail";
    }

    /**
     * 修改界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("updatePerm/{id}")
    public String updatePerm(@PathVariable Long id , Model model) {
        Dto dto = new Dto();
        dto.put("id" , id);
        SysPermission perm = permissionService.selectPerm(dto);
        model.addAttribute("permission" , perm);
        return "permission/update";
    }

    /**
     * 修改权限
     * @param id
     * @param name
     * @param permission
     * @param available
     * @param url
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public Map<String ,Object> update(Long id , String name , String permission , String available , String url) {
        Map<String ,Object> map = new HashMap<>();
        SysPermission perm = new SysPermission();
        perm.setId(id);
        perm.setName(name);
        perm.setPermission(permission);
        perm.setUrl(url);
        perm.setAvailable(Boolean.valueOf(available));
        permissionService.updatePermission(perm);

        map.put("code" , 0);
        map.put("msg" , "修改成功");
        return map;
    }

    /**
     * 删除一个权限
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Map<String ,Object> delete(Long permission_id) {
        Map<String , Object> map = new HashMap<>();
        permissionService.deletePermission(permission_id);
        map.put("code" , 0);
        map.put("msg" , "删除权限成功");
        return map;
    }
}
